layui.use(['form', 'util'], function () {
    //获取iframe的body元素
    var form = layui.form;
    var util = layui.util

    //处理属性 为 lay-active 的所有元素事件
    util.event('lay-active', {
        show: function (othis) {
            var data = othis[0].dataset
            parent.layer.open({
                type: 2
                ,
                title: data.name + data.year + '年报告'
                ,
                closeBtn: false
                ,
                area: ['90%', '90%']
                // ,shade: 0.8
                , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                ,
                btn: ['关闭']
                ,
                btnAlign: 'c'
                ,
                moveType: 1 //拖拽模式，0或者1
                ,
                moveOut: true
                ,
                closeBtn: 1
                ,
                // content: getRootPath() + '/static/plugins/pdfjs/web/viewer.html?file=' + getRootPath() + '/report/' + data.id
                content: getRootPath() + '/report/' + data.id
                ,
                success: function (layero) {
                    //点击按钮 do something
                }
            });
        }
        , grade: function (othis) {
            var data = othis[0].dataset
            $("#uid").val(data.id);
            var ind = layer.open({
                //基本层类型
                type: 1,
                shadeClose: false,
                scrollbar: false,
                area: ['350px', '225px'],
                //标题
                title: data.name + data.year + "年报告打分",
                //内容
                content: $('#commentForm'),
                //按钮
                btn: ['确定打分', '取消'],
                //确定按钮
                yes: () => {
                    if (validateCode()) {
                        var values = document.getElementById("score").value;
                        if (values <= 60) {
                            layer.prompt({
                                formType: 2,
                                value: '',
                                title: '请输入管理服务改建建议',
                                area: ['350px', '150px'] //自定义文本域宽高
                            }, function (value, index, elem) {
                                if (value == null || value == "") {
                                    layer.msg("请输入管理服务改建建议")
                                } else {
                                    document.getElementById("context").value = value
                                    //开启loading
                                    const loading = layer.load(2);
                                    //获取表单内的所有值
                                    var formObj = form.val('scoreForm');
                                    $.ajax({
                                        url: '/task/grade',
                                        method: "post",
                                        contentType: 'application/json;charset=utf-8',
                                        data: JSON.stringify(formObj),
                                        dataType: "json",
                                        success: function (data) {
                                            layer.close(loading);
                                            if (data.code == 200) {
                                                layer.msg("打分成功！", {icon: 6, time: 1000});
                                                setTimeout(function () {
                                                    window.location.href = getRootPath() + '/score'
                                                }, 1100);
                                                layer.close(ind);
                                            } else {
                                                layer.alert(data.msg, {icon: 5, time: 1500});
                                                layer.close(ind);
                                            }
                                        },
                                        error: function () {
                                            layer.alert('连接服务器失败...', {icon: 5, time: 1500});
                                        }
                                    });
                                    layer.close(index);
                                }
                            });
                        }
                        else {
                            //开启loading
                            const loading = layer.load(2);
                            //获取表单内的所有值
                            var formObj = form.val('scoreForm');
                            $.ajax({
                                url: '/task/grade',
                                method: "post",
                                contentType: 'application/json;charset=utf-8',
                                data: JSON.stringify(formObj),
                                dataType: "json",
                                success: function (data) {
                                    layer.close(loading);
                                    if (data.code == 200) {
                                        layer.msg("打分成功！", {icon: 6, time: 1000});
                                        setTimeout(function () {
                                            window.location.href = getRootPath() + '/score'
                                        }, 1100);
                                        layer.close(ind);
                                    } else {
                                        layer.alert(data.msg, {icon: 5, time: 1500});
                                        layer.close(ind);
                                    }
                                },
                                error: function () {
                                    layer.alert('连接服务器失败...', {icon: 5, time: 1500});
                                }
                            });
                        }
                    }
                },
            });
        }
    });

});

function validateCode() {
    var value = $("#score").val();
    if (value == "" || value == null) {
        layer.msg("请输入分数！", {icon: 5, time: 1500});
        return false
    }
    if (value > 100) {
        layer.msg("不能超过100分哟！", {icon: 5, time: 1500});
        return false
    }
    return true;
}

function getRootPath() {
    // 获取当前网址，如：http://localhost:8080/MyWeb/index.html
    var curWwwPath = window.document.location.href;
    // 获取主机地址之后的目录，如： MyWeb/index.html
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    // 获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    return localhostPath;
}


